var express = require('express');
var router = express.Router();
var qiniu = require("qiniu");

var uploadCounter = 0;

/* GET users listing. */
router.get('/get_token', function(req, res, next) {

  var fileNameSPlited = req.query.fileName.split(".");
  var extName = fileNameSPlited[fileNameSPlited.length - 1];

  var accessKey = 'wJtYxHE-67mY80w_cakrXD1BAeqfUzxzw8_33o52';
  var secretKey = 'X1Bu8hVDDFPJcZTbbHDgPtWfe4z6wvgbICyWpuEJ';
  var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

  var fileKey = "f" + ( ++ uploadCounter ) + "@" + new Date().getTime() + "." + extName;

  var options = {
    scope: "love-nature:" + fileKey
  };
  var putPolicy = new qiniu.rs.PutPolicy(options);
  var uploadToken=putPolicy.uploadToken(mac);

  res.json({
    token: uploadToken,
    fileKey: fileKey
  })

});

module.exports = router;
